#!/bin/bash

# 测试团队管理API的脚本

BASE_URL="http://localhost:8080/api"
ADMIN_EMAIL="admin@rainbond.com"
ADMIN_PASSWORD="admin123"

echo "=== Rainbond Console Java - 团队管理API测试 ==="

# 1. 登录获取Token
echo "1. 登录获取Token..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/login" \
  -H "Content-Type: application/json" \
  -d "{
    \"email\": \"$ADMIN_EMAIL\",
    \"password\": \"$ADMIN_PASSWORD\"
  }")

echo "登录响应: $LOGIN_RESPONSE"

# 提取Token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
  echo "❌ 登录失败，无法获取Token"
  exit 1
fi

echo "✅ 登录成功，Token: ${TOKEN:0:20}..."

# 2. 创建团队
echo -e "\n2. 创建团队..."
TEAM_RESPONSE=$(curl -s -X POST "$BASE_URL/teams" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "tenantName": "test-team",
    "tenantAlias": "测试团队",
    "description": "这是一个测试团队",
    "enterpriseId": "default-enterprise"
  }')

echo "创建团队响应: $TEAM_RESPONSE"

# 3. 获取团队列表
echo -e "\n3. 获取团队列表..."
TEAMS_RESPONSE=$(curl -s -X GET "$BASE_URL/teams" \
  -H "Authorization: Bearer $TOKEN")

echo "团队列表响应: $TEAMS_RESPONSE"

# 4. 获取团队详情
echo -e "\n4. 获取团队详情..."
TEAM_DETAIL_RESPONSE=$(curl -s -X GET "$BASE_URL/teams/test-team" \
  -H "Authorization: Bearer $TOKEN")

echo "团队详情响应: $TEAM_DETAIL_RESPONSE"

# 5. 获取团队总览
echo -e "\n5. 获取团队总览..."
TEAM_OVERVIEW_RESPONSE=$(curl -s -X GET "$BASE_URL/teams/test-team/overview" \
  -H "Authorization: Bearer $TOKEN")

echo "团队总览响应: $TEAM_OVERVIEW_RESPONSE"

# 6. 获取团队成员
echo -e "\n6. 获取团队成员..."
TEAM_USERS_RESPONSE=$(curl -s -X GET "$BASE_URL/teams/test-team/users" \
  -H "Authorization: Bearer $TOKEN")

echo "团队成员响应: $TEAM_USERS_RESPONSE"

# 7. 创建应用组
echo -e "\n7. 创建应用组..."
APP_GROUP_RESPONSE=$(curl -s -X POST "$BASE_URL/teams/test-team/groups" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "groupName": "test-app-group",
    "groupAlias": "测试应用组",
    "description": "这是一个测试应用组",
    "regionName": "default-region",
    "isPublic": false,
    "isGovernance": false
  }')

echo "创建应用组响应: $APP_GROUP_RESPONSE"

# 8. 获取应用组列表
echo -e "\n8. 获取应用组列表..."
APP_GROUPS_RESPONSE=$(curl -s -X GET "$BASE_URL/teams/test-team/groups" \
  -H "Authorization: Bearer $TOKEN")

echo "应用组列表响应: $APP_GROUPS_RESPONSE"

echo -e "\n=== 测试完成 ==="





